package org.jeecg.modules.job.api.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.job.ums.entity.UmsShareLog;
import org.jeecg.modules.job.ums.service.IUmsShareLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 *分享模块管理
 */
@RestController
@RequestMapping("/api/share")
@Api(tags="移动端分享管理模块")
@Slf4j
public class ShareController{

    @Autowired
    private IUmsShareLogService shareLogService;

    /**
     * 查询分享列表
     */
    @RequestMapping(value = "/getShareList",method = RequestMethod.GET)
    @ApiOperation(value = "查询分享列表", notes = "查询分享列表")
    public Result<Object> getTaskList(UmsShareLog param,
                                      @RequestParam(name="pageNo", defaultValue="1") Integer page,
                                      @RequestParam(name="limit", defaultValue="10") Integer limit
    ) {
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        param.setMemberId(user.getId());
        IPage<UmsShareLog> pageInfo= shareLogService.findPageBySpec(new Page<>(page,limit),param);
        return Result.OK(pageInfo);
    }

    /**
     * 查询分享详情
     */
    @RequestMapping(value = "/getShareDetail",method = RequestMethod.GET)
    @ApiOperation(value = "查询分享详情", notes = "查询分享详情")
    public Result<Object> getShareDetail(String id) {
        UmsShareLog result=shareLogService.getById(id);
        return Result.OK(result);
    }

    /**
     * 添加/更新分享
     */
    @RequestMapping(value = "/addOrUpdateShare",method = RequestMethod.POST)
    @ApiOperation(value = "添加/更新分享", notes = "添加/更新分享")
    public Result<Object> addOrUpdateShare(@RequestBody UmsShareLog param) {
        try {
            LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            param.setMemberId(user.getId());
            shareLogService.addOrUpdate(param);
            return Result.OK();
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    /**
     * 删除分享
     */
    @RequestMapping(value = "/deleteShare",method = RequestMethod.POST)
    @ApiOperation(value = "删除分享", notes = "删除分享")
    public Result<Object> deleteShare(@RequestBody UmsShareLog param) {
        try {
            shareLogService.removeById(param.getId());
            return Result.OK();
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

}



